from unittest.case import TestCase
import builders

from mapper_integration_test import Security

__author__ = 'romilly'

class MyClass(object):
    pass

class TestClassModel(TestCase):
    def setUp(self):
        self.cm = builders.ClassModelBuilder(MyClass).withField("one").withField("two").build()

    def testClassModelCanCreateAndInitialiseAnInstance(self):
        inst = self.cm.buildInstance({'one': 'first', 'two' : 'second'})
        self.assertEqual(inst.one,"first")
        self.assertEqual(inst.two,"second")

class TestTableModel(TestCase):
    def setUp(self):
        self.tm = builders.TableModelBuilder('SECURITY').withColumn('NAME', 50).withColumn('TIDM', 5).withColumn('RIC', 10).withColumn('YAHOO', 10).build()

    def testCanComposeTableCreationDdl(self):
        self.assertEqual(self.tm.createTableStatement(),
                         'CREATE TABLE SECURITY(NAME VARCHAR(50), TIDM VARCHAR(5), RIC VARCHAR(10), YAHOO VARCHAR(10))')

class TestMap(TestCase):
    def setUp(self):
        self.map = builders.MapBuilder(Security).withColumn('NAME', 50).withColumn('TIDM', 5).withColumn('RIC', 10).withColumn('YAHOO', 10).build()

    def testCanGenerateInsertStatement(self):
        self.assertEqual(self.map.sqlToInsert(), "INSERT INTO SECURITY (NAME, TIDM, RIC, YAHOO) VALUES(?,?,?,?)")

    def testCanGenerateUpdateStatement(self):
        self.assertEqual(self.map.sqlToUpdate(), 'UPDATE SECURITY SET NAME=?, TIDM=?, RIC=?, YAHOO=? WHERE rowid=?')

    def testCanSelectByColumnName(self):
        self.assertEqual(self.map.sqlToSelectByColumnName('TIDM'),
                         "SELECT NAME, TIDM, RIC, YAHOO FROM SECURITY WHERE TIDM=?")

    def testCanTurnObjectIntoTupleWithRowId(self):
        security = Security('Allergy Therapeutics','AGY','AGY.L','LON:AGY')
        security.id = 1
        self.assertEqual(self.map.instanceToTupleWithId(security),('Allergy Therapeutics','AGY','AGY.L','LON:AGY', 1))

  